package net.stogi.dsper.back.dao.impl;

import java.util.List;

import net.stogi.common.dao.impl.GenericHibernateDaoImpl;
import net.stogi.dsper.api.model.User;
import net.stogi.dsper.back.dao.UserDao;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserHibernateDaoImpl extends GenericHibernateDaoImpl<User, Long> implements UserDao {

	@Autowired
	public UserHibernateDaoImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@Override
	@SuppressWarnings("unchecked")
	public User loadByUsername(String username) {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);

		criteria.add(Restrictions.eq("login", username));

		List<User> result = getHibernateTemplate().findByCriteria(criteria);

		if (result.size() > 0) {
			return result.get(0);
		}
		return null;
	}
}
